package com.woniu.venue.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniu.exception.venueNotice.VenueNoticeException;
import com.woniu.exception.venueNotice.VenueNoticeExceptionCode;
import com.woniu.venue.dto.venueNotice.VenueNoticeDto;
import com.woniu.venue.model.VenueNotice;
import com.woniu.venue.dao.VenueNoticeDao;
import com.woniu.venue.param.venueNotice.AddNoticeParam;
import com.woniu.venue.param.venueNotice.SearchNoticeParam;
import com.woniu.venue.param.venueNotice.UpdateNoticeParam;
import com.woniu.venue.service.VenueNoticeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 杜岷芫
 * @since 2024年07月12日
 */
@Service
public class VenueNoticeServiceImpl extends ServiceImpl<VenueNoticeDao, VenueNotice> implements VenueNoticeService {

    @Autowired
    private VenueNoticeDao venueNoticeDao;
    
    @Override
    public VenueNoticeDto updateNotice(UpdateNoticeParam param) {
        VenueNotice venueNotice = venueNoticeDao.selectById(param.getId());
        if (ObjectUtil.isEmpty(venueNotice))
            throw new VenueNoticeException(VenueNoticeExceptionCode.VENUE_NOTICE_NOT_EXIST);
        if (ObjectUtil.isNotEmpty(param.getNoticeImage()))
            venueNotice.setNoticeImage(param.getNoticeImage());
        if (ObjectUtil.isNotEmpty(param.getNoticeContent()))
            venueNotice.setNoticeContent(param.getNoticeContent());
        if (ObjectUtil.isNotEmpty(param.getNoticeName()))
            venueNotice.setNoticeName(param.getNoticeName());
        venueNoticeDao.updateById(venueNotice);
        VenueNoticeDto venueNoticeDto = BeanUtil.toBean(venueNotice, VenueNoticeDto.class);
        return venueNoticeDto;
    }

    @Override
    public VenueNoticeDto addNotice(AddNoticeParam param) {
        VenueNotice venueNotice = BeanUtil.toBean(param, VenueNotice.class);
        // 获取当前时间
        venueNotice.setNoticePublishTime(DateUtil.format(LocalDateTime.now(), "yyyy-MM-dd HH:mm:ss"));
        venueNoticeDao.insert(venueNotice);
        VenueNoticeDto venueNoticeDto = BeanUtil.toBean(venueNotice, VenueNoticeDto.class);
        return venueNoticeDto;
    }

    @Override
    public void deleteNotice(Integer id) {
        venueNoticeDao.deleteById(id);
    }

    @Override
    public PageInfo<VenueNoticeDto> searchNotice(SearchNoticeParam param) {
        PageHelper.startPage(param.getPageNum() == null ? 1 : param.getPageNum(), param.getPageSize() == null ? 6 : param.getPageSize());
        List<VenueNoticeDto> venueNoticeDtos = venueNoticeDao.load(param);
        PageInfo<VenueNoticeDto> venueNoticeDtoPageInfo = new PageInfo<>(venueNoticeDtos);
        return venueNoticeDtoPageInfo;
    }

    @Override
    public List<VenueNoticeDto> loadForUser() {
        QueryWrapper<VenueNotice> qw = new QueryWrapper<VenueNotice>().orderByDesc("notice_publish_time").last("limit 0,10");
        List<VenueNotice> venueNotices = venueNoticeDao.selectList(qw);
        List<VenueNoticeDto> venueNoticeDtos = BeanUtil.copyToList(venueNotices, VenueNoticeDto.class);
        return venueNoticeDtos;
    }

    @Override
    public VenueNoticeDto showNotice(Integer id) {
        //如果不用评论
        VenueNotice venueNotice = venueNoticeDao.selectById(id);
        VenueNoticeDto venueNoticeDto = BeanUtil.toBean(venueNotice, VenueNoticeDto.class);
        return venueNoticeDto;
    }

}
